package com.serverTechnology.teamsGroups.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.serverTechnology.teamsGroups.entity.TeLocationType;
import com.serverTechnology.teamsGroups.entity.TeLocationTypeParam;
import com.serverTechnology.teamsGroups.entity.dto.LocationTypeAddDto;
import com.serverTechnology.teamsGroups.entity.dto.TeLocationTypeDto;
import com.serverTechnology.teamsGroups.entity.vo.TeLocationTypeVo;
import com.serverTechnology.teamsGroups.mapper.TeLocationTypeMapper;
import com.serverTechnology.teamsGroups.service.LocationTypeParamService;
import com.serverTechnology.teamsGroups.service.LocationTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.print.PageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 库位类型 服务实现类
 * </p>
 *
 * @author 王凯炫
 * @since 2024-08-05
 */
@Service
public class TeLocationTypeServiceImpl extends ServiceImpl<TeLocationTypeMapper, TeLocationType> implements LocationTypeService {
    @Autowired
    private LocationTypeParamService teLocationTypeParamService;
    @Resource
    private TeLocationTypeMapper teLocationTypeMapper;

    /**
     * 添加库位类型
     * @param locationTypeAddDto
     */
    @Override
    @Transactional
    public void addTeLocationType(LocationTypeAddDto locationTypeAddDto) {

        TeLocationType teLocationType = new TeLocationType();
        BeanUtils.copyProperties(locationTypeAddDto, teLocationType);
            try {
                // 判断库位类型编码是否重复  添加或修改
                boolean b = ObjectUtils.isEmpty(locationTypeAddDto.getId()) ?
                        this.save(teLocationType) : this.updateById(teLocationType);
            } catch (Exception e) {
                throw new RuntimeException("库位类型编码重复");
        }
            //判断是否添加库位类型参数
        if(ObjectUtils.isNotEmpty(locationTypeAddDto.getCraftParam())){
            extracted(locationTypeAddDto, teLocationType);
        }
    }

    /**
     * 添加修改库位类型参数表
     * @param locationTypeAddDto
     * @param teLocationType
     */
    private void extracted(LocationTypeAddDto locationTypeAddDto, TeLocationType teLocationType) {
        ArrayList<TeLocationTypeParam> teLocationTypeParams = new ArrayList<>();
        //循环拷贝数据
        for (LocationTypeAddDto.CraftParam craftParam : locationTypeAddDto.getCraftParam()) {
            TeLocationTypeParam teLocationTypeParam = new TeLocationTypeParam();
            teLocationTypeParam.setId(craftParam.getId());
            teLocationTypeParam.setLocationTypeId(teLocationType.getId());
            teLocationTypeParam.setParamName(craftParam.getParamName());
            teLocationTypeParam.setParamCode(craftParam.getParamCode());
            teLocationTypeParam.setType(craftParam.getType());
            teLocationTypeParams.add(teLocationTypeParam);
        }
        //添加或修改
        teLocationTypeParamService.saveOrUpdateBatch(teLocationTypeParams);
    }

    /**
     * 分页查询
     * @param locationTypeDto
     * @return
     */
    @Override
    public PageResult<TeLocationTypeVo> pages(TeLocationTypeDto locationTypeDto) {
        MPJLambdaWrapper<TeLocationType> queryWrapper = new MPJLambdaWrapper<>();

        // 添加查询条件
        queryWrapper.like(ObjectUtils.isNotEmpty(locationTypeDto.getName()), TeLocationType::getName, locationTypeDto.getName())
                .selectAll(TeLocationType.class);

        // 添加左连接并选择字段
        queryWrapper.leftJoin(TeLocationTypeParam.class, TeLocationTypeParam::getLocationTypeId, TeLocationType::getId)
                .selectCollection(TeLocationTypeParam.class,TeLocationTypeVo::getCraftParam, map -> map.result(TeLocationTypeParam::getParamName));
        // 获取分页结果
        Page<TeLocationTypeVo> teLocationTypeVoPage = teLocationTypeMapper.selectJoinPage(
                PageFactory.defaultPage(),
                TeLocationTypeVo.class,
                queryWrapper
        );

        // 将分页结果转换为 PageResult 对象
        return PageResultFactory.createPageResult(teLocationTypeVoPage);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public TeLocationTypeVo getByIds(Long id) {
        MPJLambdaWrapper<TeLocationType> teLocationTypeMPJLambdaWrapper = new MPJLambdaWrapper<>();
        teLocationTypeMPJLambdaWrapper.selectAll(TeLocationType.class)
                .leftJoin(TeLocationTypeParam.class, TeLocationTypeParam::getLocationTypeId, TeLocationType::getId)
                // 选择字段
                .selectCollection(TeLocationTypeParam.class,TeLocationTypeVo::getCraftParam,map -> map .result(TeLocationTypeParam::getParamName))
                .eq(TeLocationType::getId,id);
        TeLocationTypeVo teLocationTypeVo = teLocationTypeMapper.selectJoinOne(TeLocationTypeVo.class, teLocationTypeMPJLambdaWrapper);
        return teLocationTypeVo;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void dels(Long id) {
        LambdaQueryWrapper<TeLocationTypeParam> locationTypeId = new LambdaQueryWrapper<TeLocationTypeParam>().in(TeLocationTypeParam::getLocationTypeId, id);
        List<Long> collect = teLocationTypeParamService.list(locationTypeId).stream().map(TeLocationTypeParam::getId).collect(Collectors.toList());
        teLocationTypeParamService.removeByIds(collect);
        this.remove(new LambdaQueryWrapper<TeLocationType>().eq(TeLocationType::getId,id));
    }


}
